package com.study.bigwork.circle.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;

import com.google.gson.Gson;

import com.study.bigwork.entity.Circle;
import com.study.bigwork.entity.CircleComment;
import com.study.bigwork.entity.User;
import com.study.bigwork.user.service.UserService;

@Repository
public class CircleDao {
	@Resource
	private SessionFactory sessionFactory;
	public boolean addCircle(Circle circle) {
		Session session = this.sessionFactory.getCurrentSession();
		Gson gson = new Gson();
		if(circle.getSendImg()!=null) {
			String str=gson.toJson(circle.getSendImg());
			circle.setSendImg1(str);
		}
		session.save(circle);
		return true;
	}
	public List<Circle> getCircles(){
		Session session=this.sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Circle");
		List<Circle> circles=query.getResultList();
		for(int i=0;i<circles.size();i++) {
			Circle circle=circles.get(i);
			circle.setSendImg();
		}
		if(circles!=null) {
			return circles;
		}else {
			return null;
		}
		
	}
	
	
	
	
	
	
	
	
	/**
	 * 新增帖子评论
	 * @param order 待添加的帖子对象
	 * @return 添加帖子是否成功，成功返回true，否则返回false
	 */
	public boolean addCircleComment(int circle_id,int comment_id,String comment) {
		CircleComment circleComment=new CircleComment();
		circleComment.setContent(comment);
		Session session=this.sessionFactory.getCurrentSession();
		session.save(circleComment);
		String hql="update CircleComment set circle_id=?,comment_id=? where comment=?";
		Query query=session.createQuery(hql);
		query.setParameter(0, circle_id);
		query.setParameter(1, comment_id);
		query.setParameter(2, comment);
		query.executeUpdate();
		return true;
	}
	
	/**
	 * 获取朋友圈评论
	 * @param sql
	 * @return
	 */
	public List<CircleComment> getCircleComments(int circleId){
		
		List<CircleComment> comments;
		System.out.println("获取到的circleId为："+circleId);
		//查询circle_comment
		Session session=sessionFactory.getCurrentSession();
		String hql="from CircleComment where circle_id=?";
		Query query=session.createQuery(hql);
		query.setParameter(0, circleId);
		comments=query.getResultList();
		for(int i=0;i<comments.size();i++) {
			UserService us = new UserService();
			int id=comments.get(i).getId();
			String image=us.findImageById(id);
			String name=us.findNameById(id);
			comments.get(i).setImage(image);
			comments.get(i).setCommentName(name);
		}
		return comments;
	}
	/**
	 * 每次添加完评论后评论数+1
	 */
	public void addCommentSize(int id) {
		Session session = this.sessionFactory.getCurrentSession();
		//改成sql语句 
		String hql = "update Circle set commentSize=commentSize+1 where id=?";
		Query query = session.createQuery(hql);
		query.setParameter(0,id);
		query.executeUpdate();
	}
	
	/**
	 * 每次删除完评论后评论数-1
	 */
	public void reduceCommentSize(int id) {
		Session session = this.sessionFactory.getCurrentSession();
		//改成sql语句 
		String hql = "update Circle set commentSize=commentSize-1 where id=?";
		Query query = session.createQuery(hql);
		query.setParameter(0,id);
		query.executeUpdate();
	}
	
	/**
	 * 根据评论id删除评论
	 */
	public int deleteCommentById(int id) {

		Session session = this.sessionFactory.getCurrentSession();
		String hql="delete from CircleComment where id=?";
		Query query=session.createQuery(hql);
		query.executeUpdate();
		return 1;
	}
	
	/**
	 根据评论id查找circleId
	 */
	public int selectCircleIdById(int id) {
		int circleId=0;
		String hql="from CircleComment where id=?";
		Session session=sessionFactory.getCurrentSession();
		Query query=session.createQuery(hql);
		Circle circle=(Circle) query.uniqueResult();
		circleId=circle.getId();
		
		return circleId;
	}
}
